//
//  mappingViewController.m
//  MoneyLog
//
//  Created by wicresoft on 5/11/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "mappingViewController.h"


@implementation MarkAnnotation

@synthesize coordinate,title,subtitle;

-(id) initWithCoordinate:(CLLocationCoordinate2D )aCoordinate title:(NSString *)aTitle subtitle:(NSString *)aSubTitle{
    if (self == [super init]) {
        coordinate = aCoordinate;
        self.title = aTitle;
		self.subtitle = aSubTitle;
    }
    return self;
} 

- (id) initWithLatitude: (CLLocationDegrees)aLatitude longitude: (CLLocationDegrees)aLongitude title:(NSString *)aTitle subTitle:(NSString *)aSubTitle
{
	if(self == [super init])
	{
		CLLocationCoordinate2D curCoordinate;
		curCoordinate.latitude = aLatitude;
		curCoordinate.longitude = aLongitude;
		coordinate = curCoordinate;
		
		self.title = aTitle;
		self.subtitle = aSubTitle;
	}
	return self;
}


-(void)dealloc{
    [title release];
    [subtitle release];
    
    [super dealloc];
}

@end


@implementation mappingViewController
@synthesize _mapView;
@synthesize coordinate;
@synthesize mapMark;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [_mapView release];
    [mapMark release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _mapView = [[MKMapView alloc] initWithFrame:[self.view bounds]];
    _mapView.mapType = MKMapTypeStandard;
    //_mapView.showsUserLocation = YES;
    
    float zoomLevel = 0.018;
    MKCoordinateRegion region =MKCoordinateRegionMake(coordinate,MKCoordinateSpanMake(zoomLevel,zoomLevel));
    [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
    
    mapMark = [[MarkAnnotation alloc] initWithCoordinate:self.coordinate title:@"Title" subtitle:@"Subtitle"];
    [_mapView addAnnotation:mapMark];
    
    [self.view addSubview:_mapView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - * MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
	MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"locationMark"];
    
	[view setPinColor:MKPinAnnotationColorPurple];
	UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
	[button setFrame:CGRectMake(0, 0, 20, 20)];
	[button addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
	
    view.rightCalloutAccessoryView = button;
	view.canShowCallout = YES;
	view.animatesDrop = YES;
    
	return view;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    //[self getCurrentCity];
    // we have received our current location, so enable the "Get Current Address" button
}

#pragma mark - * MKReverseGeocoderDelegate

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"MKReverseGeocoder has failed.");
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    //currentCityLabel.text = placemark.locality;
}


//-(void) getCurrentCity
//{
//    self.reverseGeocoder =[[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
//    
//    NSLog(@"%g",mapView.userLocation.location.coordinate.latitude);
//    NSLog(@"%g",mapView.userLocation.location.coordinate.longitude);
//    
//    
//    reverseGeocoder.delegate = self;
//    [reverseGeocoder start];
//}

#pragma mark - * actions

- (void)showDetails: (id)sender
{
    MKCoordinateRegion region = MKCoordinateRegionMake(mapMark.coordinate,MKCoordinateSpanMake(0.015, 0.015));
    
	[_mapView setRegion:region animated:YES];
}


@end
